<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>H+ 后台主题UI框架 - 数据表格</title>
    <link rel="shortcut icon" href="${pageContext.request.contextPath}/favicon.ico">
    <link href="${pageContext.request.contextPath}/static/css/bootstrap.min.css?v=3.3.6" rel="stylesheet">
    <link href="${pageContext.request.contextPath}/static/css/font-awesome.css?v=4.4.0" rel="stylesheet">
    <!-- Data Tables -->
    <link href="${pageContext.request.contextPath}/static/css/plugins/dataTables/dataTables.bootstrap.css"
          rel="stylesheet">
    <link href="${pageContext.request.contextPath}/static/css/animate.css" rel="stylesheet">
    <link href="${pageContext.request.contextPath}/static/css/style.css?v=4.1.0" rel="stylesheet">

    <!-- 全局js -->
    <script src="${pageContext.request.contextPath}/static/js/jquery.min.js?v=2.1.4"></script>
    <script src="${pageContext.request.contextPath}/static/js/bootstrap.min.js?v=3.3.6"></script>
    <script src="${pageContext.request.contextPath}/static/js/plugins/jeditable/jquery.jeditable.js"></script>
    <!-- Data Tables -->
    <script src="${pageContext.request.contextPath}/static/js/plugins/dataTables/jquery.dataTables.js"></script>
    <script src="${pageContext.request.contextPath}/static/js/plugins/dataTables/dataTables.bootstrap.js"></script>
    <!-- 自定义js -->
    <script src="${pageContext.request.contextPath}/static/js/content.js?v=1.0.0"></script>

</head>

<body class="gray-bg">
<div class="wrapper wrapper-content animated fadeInRight">
    <div class="row">
        <div class="col-sm-12">
            <div class="ibox float-e-margins">
                <div >
                    <div style=" float: left;margin-left: 6%">
                        <a onclick=" " href="" class="btn btn-primary "><i class="fa fa-refresh"></i></a>
                    </div>
                    <div style=" float: right">
                        <input type="text" style="width: 200px;height: 30px;" id="selectStudentName"
                               placeholder="按学生名字查询，输入关键字即可">
                        <button class="btn btn-primary" onclick="selectStudent()">查询</button>
                    </div>
                </div>

                <div class="ibox-content">
                    <table class="table table-striped table-bordered table-hover " id="" style="text-align: center;width: 90%;margin-left: 5%;margin-right: 5%">
                        <thead>
                        <tr>
                            <th style="width: 12%">学生学号</th>
                            <th style="width: 12%">学生姓名</th>
                            <th style="width: 12%">班级</th>
                            <th style="width: 12%">课程</th>
                            <th style="width: 12%">授课教师</th>
                            <th style="width: 12%">平时成绩</th>
                            <th style="width: 12%">考试成绩</th>
                            <th style="width: 12%">操作</th>
                        </tr>
                        </thead>
                        <tbody id="tb">
                        <c:forEach var="studentCourse" items="${studentCourses}">
                             <tr>
                                 <td>${studentCourse.studentId}</td>
                                 <td>${studentCourse.studentName}</td>
                                 <td>${studentCourse.className}</td>
                                 <td class="center">${studentCourse.courseName}</td>
                                 <td>${studentCourse.teacherName}</td>
                                 <td>${studentCourse.scoreGPA}</td>
                                 <td class="center">${studentCourse.scoreExam}</td>
                                 <td class="center">
                                     <button class="btn btn-primary " onclick="updateStudentCourse(${studentCourse.studentId})"><i class="fa fa-edit"></i></button>
                                     <button class="btn btn-primary " onclick="deleteStudentScoreByStudentId(${studentCourse.studentId})"><i class="fa fa-trash"></i></button>
                                 </td>
                             </tr>
                         </c:forEach>
                        </tbody>
                    </table>
                    <div class="margin" style="margin-left: 5%">
                        每一页显示<select id="pageSizeSelect" onchange="onSelectChange()">
                        <option value="2">2</option>
                        <option value="5">5</option>
                        <option value="10">10</option>
                    </select> 条数据，
                        当前是第<span id="currentPageSpan"></span> 页，
                        总共<span id="totalPageSpan"></span> 页
                        <button class="button" onclick="firstPage()">首页</button>
                        <button class="button" onclick="prePage()">上一页</button>
                        <button class="button" onclick="nextPage()">下一页</button>
                        <button class="button" onclick="lastPage()">尾页</button>
                    </div>
                </div>
            </div>
        </div>
    </div>
</div>
<%--添加学生成绩模态框--%>
<div class="modal" id="editModal">
    <%--对话模态框--%>
    <div class="modal-dialog">
        <div class="modal-content">
            <div class="modal-header">
                <h4>编辑学生成绩</h4>
            </div>
            <div class="modal-body">
                <input type="hidden" id="studentIdInput">
                <input type="hidden" id="scoreIdInput">
                <input type="hidden" id="courseIdInput">
                <label class="control-label">课程</label>
                <input class="form-control" type="text" id="courseNameInput" placeholder="请输入课程">
                <label class="control-label">平时成绩</label>
                <input class="form-control" type="text" id="scoreGPAInput" placeholder="请输入平时成绩">
                <label class="control-label">考试成绩</label>
                <input class="form-control" type="text" id="scoreExamInput" placeholder="请输入考试成绩">
            </div>
            <div class="modal-footer">
                <button class="button btn-primary" data-dismiss="modal">取消</button>
                <button class="button btn-primary" onclick="editCourse()">确定</button>
            </div>
        </div>
    </div>
</div>



<!-- Page-Level Scripts -->
<script>
    /*在学生成绩展示页面实现对学生成绩信息模糊查询*/
    function showList(studentList) {
        console.log(JSON.stringify(studentList));
        var html = '';
        //遍历studentList数组
        for (var i = 0; i < studentList.length; i++) {
            var item = studentList[i];
            console.log(JSON.stringify(studentList[i]));
            html += "<tr>";
            html += "<td>" + item.studentId + "</td>";//显示用户的id
            html += "<td>" + item.studentName + "</td>";//显示用户的姓名
            html += "<td>" + item.className + "</td>";
            html += "<td>" + item.courseName + "</td>";
            html += "<td>" + item.teacherName + "</td>";
            html += "<td>" + item.scoreGPA + "</td>";
            html += "<td>" + item.scoreExam + "</td>";
            html += "<td><button class='btn btn-primary' onclick='updateStudentCourse(\"" + item.studentId + "\")'><i class=\"fa fa-edit\"></i></button>" +
                "<button class='btn btn-primary' onclick='deleteStudentScoreByStudentId(\"" + item.studentId + "\")'><i class=\"fa fa-trash\"></i></button></td>";
            html += "</tr>";
        }
        $("#tb").html(html);
    }

    /*模糊查询*/
    function selectStudent() {
        var studentNameValue = $("#selectStudentName").val();
        $.ajax({
            url: "${pageContext.request.contextPath}/studentCourse/getStudentByName",
            type: 'post',
            dataType: 'json',
            data: {
                "studentNameValue": studentNameValue
            },
            success: function (result) {
                if (result.code == 0) {
                    showList(result.studentList)
                }
            },
            error: function (err) {
                console.log(JSON.stringify(err));
            }
        });
    }

    //逻辑删除一个学生的成绩信息
    function deleteStudentScoreByStudentId(studentId) {
        $.ajax({
            url: "${pageContext.request.contextPath}/studentCourse/deleteStudentScoreByStudentId",
            type: 'post',
            dataType: 'json',
            data: {
                "studentId": studentId,
            },
            success: function (result) {
                if (result.code == 0) {
                    //再次分页加载数据并显示在页面
                    parent.layer.msg("删除成功", {icon: 1, time: 2000});//成功提示
                    window.location.reload()
                } else {
                    alert(result.message);
                }
            },
            error: function (err) {
                console.log(JSON.stringify(err));
            }
        })
    }


    /*成绩编辑模态框原始数据获取*/
    function updateStudentCourse(studentId) {
        $.ajax({
            url: "${pageContext.request.contextPath}/studentCourse/getCourseByStudentId",
            type: 'get',
            data: {"studentId": studentId},
            dataType: 'json',
            success: function (result) {
                if (result.code == 0) {
                    showStudentCourse(result.studentCourse);
                } else {
                    alert(result.message);
                }
            },
            error: function (err) {
                console.log(JSON.stringify(err));
            }
        });

    }

    /*显示编辑模态框*/
    function showStudentCourse(studentCourse) {
        if (studentCourse != null){
            $("#scoreIdInput").val(studentCourse.scoreId);
            $("#courseIdInput").val(studentCourse.courseId);
            $("#courseNameInput").val(studentCourse.courseName);
            $("#scoreGPAInput").val(studentCourse.scoreGPA);
            $("#scoreExamInput").val(studentCourse.scoreExam);
        }else{
            $("#scoreIdInput").val(studentCourse.scoreId);
            $("#courseIdInput").val(studentCourse.courseId);
            $("#courseNameInput").val("");
            $("#scoreGPAInput").val(0);
            $("#scoreExamInput").val(0);
        }
        $("#editModal").modal("show");
    }

    /*成绩编辑的实现*/
    function editCourse() {
        var courseNameInputValue = $("#courseNameInput").val();
        var scoreGPAInputValue = $("#scoreGPAInput").val();
        var scoreExamInputValue = $("#scoreExamInput").val();
        var scoreIdInputValue = $("#scoreIdInput").val();
        var courseIdInputValue = $("#courseIdInput").val();
        var scoreCourseDataValue = {
            courseName:courseNameInputValue,
            scoreGPA:scoreGPAInputValue,
            scoreExam:scoreExamInputValue,
            scoreId:scoreIdInputValue,
            courseId:courseIdInputValue,
        }
        $.ajax({
            url: "${pageContext.request.contextPath}/studentCourse/editScoreById",
            type: 'post',
            contentType: 'application/x-www-form-urlencoded',
            data: scoreCourseDataValue,
            dataType: 'json',
            success: function (result) {
                if (result.code == 0) {
                    parent.layer.msg("修改成功", {icon: 1, time: 2000});//成功提示
                    //显示用户编辑模态框
                    $("#editModal").modal("hide");//关闭编辑模态框
                    window.location.reload()//刷新页面
                } else {
                    alert(result.message);
                }
            },
            error: function (err) {
                console.log(JSON.stringify(err));
            }
        })
    }








    $(document).ready(function () {
        $('.dataTables-example').dataTable();

        /* Init DataTables */
        var oTable = $('#editable').dataTable();

        /* Apply the jEditable handlers to the table */
        oTable.$('td').editable('../example_ajax.php', {
            "callback": function (sValue, y) {
                var aPos = oTable.fnGetPosition(this);
                oTable.fnUpdate(sValue, aPos[0], aPos[1]);
            },
            "submitdata": function (value, settings) {
                return {
                    "row_id": this.parentNode.getAttribute('id'),
                    "column": oTable.fnGetPosition(this)[2]
                };
            },
            "width": "90%",
            "height": "100%"
        });
    });

    function fnClickAddRow() {
        $('#editable').dataTable().fnAddData([
            "Custom row",
            "New row",
            "New row",
            "New row",
            "New row"]);

    }


</script>

<script type="text/javascript" src="http://tajs.qq.com/stats?sId=9051096" charset="UTF-8"></script>
<!--统计代码，可删除-->

</body>

</html>
